package com.CST.service.impl.cb;

import com.CST.dto.cb.DateDTO;
import com.CST.entity.cb.EtlDepreciationDetail;
import com.CST.mapper.cst.cb.EtlDepreciationDetailMapper;
import com.CST.service.cb.IEtlDepreciationDetailService;
import com.CST.vo.cb.DepreciationDetailVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2022-04-22
 */
@Service
public class EtlDepreciationDetailServiceImpl extends ServiceImpl<EtlDepreciationDetailMapper, EtlDepreciationDetail> implements IEtlDepreciationDetailService {

    @Autowired
    private EtlDepreciationDetailMapper etlDepreciationDetailMapper;

    /**
     * 根据父级科室获取相应的 固定资产折旧 / 无形资产摊销 明细集合
     * 必要条件：时间+父级科室
     * unitCode = ZC057为固定资产折旧
     * unitCode = ZC058为无形资产摊销
     */
    @Override
    public List<DepreciationDetailVO> listByPDeptCode(DateDTO dateDTO, String pDeptCode) {
        LocalDate date = LocalDate.of(dateDTO.getStarYear(), dateDTO.getStarMonth(), 1);
        LocalDate nextDate = LocalDate.of(dateDTO.getEndYear(), dateDTO.getEndMonth(), 1).minusMonths(-1);
        dateDTO.setStarTime(date.toString());
        dateDTO.setEndTime(nextDate.toString());
        List<DepreciationDetailVO> depreciationDetailVOS = etlDepreciationDetailMapper.listByPDeptCode(dateDTO,
                pDeptCode, null);
        depreciationDetailVOS.forEach(e -> {
            if (e.getUsestartdate() != null)
                e.setUsestartdate(e.getUsestartdate().substring(0, 7));
        });
        return depreciationDetailVOS;
    }

    /**
     * 根据科室查询相应的明细
     * 必要条件：时间+父级科室
     * unitCode = ZC057为固定资产折旧
     * unitCode = ZC058为无形资产摊销
     *
     * @param dateDTO
     * @param deptCode
     * @return
     */
    @Override
    public List<DepreciationDetailVO> listByDeptCode(DateDTO dateDTO, String deptCode) {
        dateDTO.setYard("1");

        LocalDate startDate = LocalDate.of(dateDTO.getStarYear(), dateDTO.getStarMonth(), 1);
        LocalDate endDate = LocalDate.of(dateDTO.getEndYear(), dateDTO.getEndMonth(), 1);

       if(startDate.isEqual(endDate))
       {
           endDate =endDate.plusMonths(1);
       }

        dateDTO.setStarTime(startDate.toString());
        dateDTO.setEndTime(endDate.toString());
        List<DepreciationDetailVO> list = etlDepreciationDetailMapper.listByPDeptCode(dateDTO, null, deptCode);
        return list;
    }
}
